<?php
namespace app\manage\controller;
use think\Db;
class Faq extends Base
{
    public function index(){
        if( request()->isAjax() ){
//            $json = '{
//                "msg": "",
//    "code": 0,
//    "data": [{
//        "sd_time": "2018-02-25 17:00:07",
//        "sd_id": "1",
//        "sd_name": "测试集团",
//        "sd_sort": 1,
//        "sd_type": "2",
//        "sd_up_id": null,
//        "sd_use": 1
//    }, {
//                "sd_time": "2018-03-13 08:15:19",
//        "sd_id": "2",
//        "sd_name": "测试公司2",
//        "sd_sort": null,
//        "sd_type": null,
//        "sd_up_id": "1",
//        "sd_use": 1
//    }, {
//        "sd_time": "2018-03-13 08:15:19",
//        "sd_id": "3",
//        "sd_name": "测试公司1",
//        "sd_sort": null,
//        "sd_type": null,
//        "sd_up_id": "2",
//        "sd_use": 1
//    },{
//        "sd_time": "2018-02-25 17:00:07",
//        "sd_id": "4",
//        "sd_name": "测试集团",
//        "sd_sort": 1,
//        "sd_type": "2",
//        "sd_up_id": null,
//        "sd_use": 1
//    }, {
//                "sd_time": "2018-03-13 08:15:19",
//        "sd_id": "5",
//        "sd_name": "测试公司2",
//        "sd_sort": null,
//        "sd_type": null,
//        "sd_up_id": "4",
//        "sd_use": 1
//    }, {
//        "sd_time": "2018-03-13 08:15:19",
//        "sd_id": "6",
//        "sd_name": "测试公司1",
//        "sd_sort": null,
//        "sd_type": null,
//        "sd_up_id": "5",
//        "sd_use": 1
//    }],
//    "count": 924,
//    "is": true,
//    "tip": "操作成功！"
//}';
//            echo $json;exit;
            $id = input('id');
            $pid = input('pid');
            $keys = input('keys');
            $where['pid'] = ['<>', 0];
            $where['status'] = 1;
            !empty($id) && $where['id'] = $id;
            !empty($pid) && $where['pid'] = $pid;

            if(!empty($keys)){
                $where['title'] = ['like', "%{$keys}%"];
                $where['rid'] = 3;
                if(!$idArr = model('Faq')->where($where)->column('id')){
                    $json['data'] = [];
                    echo json_encode($json);exit;
                }

                $newArr = [];
                foreach ($idArr as $k=>$id3){
                    $id2 = model('Faq')->where('id', $id3)->value('pid');
                    $id1 = model('Faq')->where('id', $id2)->value('pid');
                    $id4 = model('Faq')->where('pid', $id3)->value('id');
                    $newArr[] = model('Faq')->where('id', 'in', [$id1, $id2, $id3, $id4])->select();
                }
                $select = [];
                foreach($newArr as $key=>$val){
                    foreach($val as $k=>$v){
                        $select[] = $v;
                    }
                }
                $count = count($select);
            }else{
                if( ! $count = model('Faq')->where($where)->count() ){
                    $json['data'] = [];
                    echo json_encode($json);exit;
                }
                $select = model('Faq')
                    ->where($where)
                    ->select();
            }

            $data = [];
            foreach ($select as $k=>$v){
                $data[] = [
                    'id' => $v['id'],
                    'pid' => $v['pid'],
                    'title' => $v['rid']==4 ? '<div style="word-wrap:break-word;height: 100%;padding-left:106px; padding-right: 5px;" class="rid">'.$v['title'].'</div>' : $v['title'],
                    'add_time' => date('Y-m-d H:i:s', $v['add_time']),
                    'rid' => $v['rid'],
                ];
            }

            $json['data'] = $data;
            $json['code'] = 0;
            $json['tip'] = 'Faq列表';
            $json['is'] = true;
            $json['count'] = $count;
            echo json_encode($json);exit;
        }else{
            return view('', ['keys'=>['充值','游戏','反馈','活动','公告']]);
        }
    }

    //添加
    public function add(){
        if( request()->isAjax() ){
            if(input('rid')==0 || input('rid')==1){
                $add0 = ['title'=>input('title'), 'add_time'=>time(), 'pid'=>input('pid'), 'rid'=>input('rid')+1];
                $addRs = model('Faq')->insert($add0);
            }else{
                $add1 = ['title'=>input('question'), 'add_time'=>time(), 'pid'=>input('pid'), 'rid'=>input('rid')+1];
                $addRs1 = model('Faq')->insertGetId($add1);

                $add2 = ['title'=>input('answer'), 'add_time'=>time(), 'pid'=>$addRs1, 'rid'=>input('rid')+2];
                $addRs = model('Faq')->insertGetId($add2);
            }

            $where['status'] = 1;
            $where['rid'] = ['in', [0, 1, 2]];
            $select = model('Faq')->where($where)->select();

            import('cate.Category');
            $faqList = \Category::unlimitedForLevel($select);
            $html = '';
            foreach ($faqList as $k=>$v){
                $selected = $v['id']==input('pid') ? 'selected' : '';
                $html .= '<option '.$selected.' value="'.$v['id'].'-'.$v['level'].'">'.str_repeat($v['html'], $v['level']).$v['title'].'</option>';
            }
            $addRs ? $this->layJson(0, '添加成功', [$html]) : $this->layJson(1, '添加失败，请重试');
        }else{
            $where['status'] = 1;
            $where['rid'] = ['in', [0, 1, 2]];
            $select = model('Faq')->where($where)->select();
            import('cate.Category');
            $faqList = \Category::unlimitedForLevel($select);
            return view('', ['faq_list'=>$faqList]);
        }
    }

    //编辑
    public function edit(){
        if( request()->isAjax() ){
            if(input('rid')==0 || input('rid')==1){
                $editRs = model('Faq')->isUpdate(true)->save(['id'=>input('id'), 'title'=>input('title')]);
            }else{
                $id = model('Faq')->where('pid', input('id'))->value('id');
                $saveAll = [
                    ['title'=>input('question'), 'id'=>input('id')],
                    ['title'=>input('answer'), 'id'=>$id],
                ];
                $editRs = model('Faq')->saveAll( $saveAll );
            }

            $editRs ? $this->layJson(0, '编辑成功') : $this->layJson(1, '编辑失败，请重试');
        }else{
            $find = model('Faq')->where('id', input('id'))->find();
            if($find['rid']==1 || $find['rid']==2){
                $findNew['question'] = '';
                $findNew['answer'] = '';
                $findNew['title'] = $find['title'];
                $findNew['pid'] = $find['pid'];
                $findNew['rid'] = $find['rid'];
            }else{
                $find2 = model('Faq')->where('pid', input('id'))->find();

                $findNew['title'] = '';
                $findNew['question'] = $find['title'];
                $findNew['answer'] = $find2['title'];
                $findNew['pid'] = $find['pid'];
                $findNew['rid'] = $find['rid'];
            }
            $where['status'] = 1;
            $where['rid'] = ['in', [0, 1, 2]];
            $select = model('Faq')->where($where)->select();
            import('cate.Category');
            $faqList = \Category::unlimitedForLevel($select);
            return view('', ['faq_list'=>$faqList, 'find'=>$findNew]);
        }
    }

    //删除
    public function del(){
        if(input('rid') == 3){
            $id = model('Faq')->where('pid', input('id'))->value('id');
            $saveAll = [
                ['status'=>-1, 'id'=>input('id')],
                ['status'=>-1, 'id'=>$id],
            ];
            $delRs = model('Faq')->saveAll( $saveAll );
        }else{
            $where['pid'] = input('id');
            $where['status'] = ['<>', -1];
            if( $count = model('Faq')->where($where)->count() ){
                $this->layJson(1, '删除失败，请先删除子分类');
            }
            $save = ['status'=>-1, 'id'=>input('id')];
            $delRs = model('Faq')->isUpdate(true)->save( $save );
        }

        $delRs!==false ? $this->layJson(0, '删除成功') : $this->layJson(1, '删除失败，请重试');
    }
}